import axios from 'axios'
// import cacheCookies from '@/utils/auth'
// 利用拦截器
const service = axios.create({
  //设置10分钟
  timeout: 600000
})

// 请求拦截器
service.interceptors.request.use(
  async (config) => {
    return config
  },
  (error) => {
    return Promise.resolve(error)
  }
)
// 响应拦截器
service.interceptors.response.use(
  async (response) => {
    return response
  },
  (error) => {
    return Promise.reject(error)
  }
)
// 再次封装
export async function http(options) {
  if (options.headers) {
    if (!options.headers['Content-Type']) {
      options.headers['Content-Type'] = 'application/json;charset:utf-8'
    }
  } else {
    options.headers = {
      'Content-Type': 'application/json;charset:utf-8'
    }
  }

  if (!options.method) {
    options.method = 'POST'
  }

  if (options.data instanceof Object) {
    options.type = 'json'
    // options.data.token = options.data.token ? options.data.token : cacheCookies.getToken()
  }
  // 处理请求参数的匹配
  if (options.method.toLowerCase() == 'get' && !options.params) {
    options.params = options.data
  }

  try {
    const response = await service(options)
    return response.data
  } catch (err) {
    return {
      status: -1,
      data: err.message
    }
  }
}
